/**
**************************************************************************************
*Founder Effect                                                                      *
*Copyright (C) 2006-2007, 5d Studios (www.5d-Studios.com)                            *
*                                                                                    *
*This library is free software; you can redistribute it and/or                       *
*modify it under the terms of the GNU Lesser General Public                          *
*License as published by the Free Software Foundation; either                        *
*version 2.1 of the License, or (at your option) any later version.                  *
*                                                                                    *
*This library is distributed in the hope that it will be useful,                     *
*but WITHOUT ANY WARRANTY; without even the implied warranty of                      *
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                   *
*Lesser General Public License for more details.                                     *
*                                                                                    *
*You should have received a copy of the GNU Lesser General Public                    *
*License along with this library; if not, write to the Free Software                 *
*Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA      *
**************************************************************************************
*/
#pragma once
#include "IConsole.h"
#include "IGameState.h"
#include "Log.h"
#include "ConsoleFunctions.h"
#include "input/InputBuffer.h"

#include <map>
#include <vector>
#include <deque>
#include <list>

// cmd stack size
#define CMD_STACK 20



/** Console display properties */
struct console_t
{
    // display setting
    float m_fraction;
    // display line
    float m_display;
    // if the console window is moving
    bool  m_moving;

    // print stack
    std::list<std::string> m_printStack;
};

/**
================================
The console allows for running commands and scripts.  
It allows for debugging and setting runtime variables
================================
*/
class Console : public IConsole, public IGameState
{
public:
    Console(void);
    /** Initialize the console */
    int Init();
    /** Shutdown */
    void Shutdown();
    /** Enter the console state */
	int Start();
    /** Exit the console state */
	void Exit();
	/** Update the console */
    void Update(float dt);

	/** Deprecated */
    void GetInput();
	/** Get the current Buffer */
    InputBuffer* GetBuffer();
    
    /** Deal with logging */
    void SetLog(bool b) { m_log = b; };
    bool IsLogging() { return m_log; };

    /**--- IConsole interface ---**/

    /** Print to the console */
    void Print( const char *s, ...);
    /** Execute a console command */
    void Exec ( const std::string &s);

    /** Display settings */
    console_t* GetDisplay() { return &m_cons; };
    /** Toggle the console activation */
    void Toggle();
    /** Test is the console is open */
    bool IsShowing() { return m_open; }

	/** Add a command */
	void AddCommand( ICommand *cmd,  const std::string &s); 
	/** Remove a command */
	void RemoveCommand( const std::string &s);

private:
    // auto complete a command
    void AutoComplete(std::string &s );
    // input buffer
    InputBuffer                m_buffer;
    // commands
    Cmds                       m_commands;
    // command stack - for scrolling through old commands
    std::deque< std::string >  cmdStack;
    int                        m_currentCmd;

    // Display for scrolling
    console_t                  m_cons;
    // Wethere the console has been initialized
    bool                       m_inited;
    bool                       m_open;

    // Loggin system
    bool                       m_log;
    Log                        m_logger;

/** -------------------------------------------
        Common Console Functions
    ------------------------------------------- **/
    Exit_f      exit_f;
    Clear_f     clear_f;
    Exec_f      exec_f;
    Set_f       set_f;
    Create_f    create_f;
    Echo_f      echo_f;
    List_f      list_f;
    Log_f       log_f;
    Run_f       run_f;
public:
    virtual ~Console(void);

};


